package com.huike.web.controller.common;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.business.service.impl.ITbBusinessTrackRecordServiceImpl;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.RecordVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.TbClueTrackRecordService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.List;

//商机和线索跟进
@RestController
@RequestMapping
public class TbTrackRecordController extends BaseController {
    @Autowired
    private ITbBusinessTrackRecordService tbBusinessTrackRecordService;

    @Autowired
    private ISysDictDataService sysDictDataService;



    @Autowired
    private TbClueTrackRecordService clueTrackRecordService;

    @Autowired
    private ITbClueService tbClueService;

    /**
     * 新增商机跟进记录
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:record:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping("/business/record")
    public AjaxResult add(@RequestBody BusinessTrackVo businessTrackVo) {
        TbBusinessTrackRecord trackRecord=new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,trackRecord);
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        TbBusiness business=new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,business);
        business.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());//进行中
        business.setId(businessTrackVo.getBusinessId());
        return toAjax(tbBusinessTrackRecordService.insertTbBusinessTrackRecord(business,trackRecord));
    }

    /**
     * 商机跟进记录列表
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:record:list')")
    @GetMapping("/business/record/list")
    public AjaxResult list(@RequestParam("businessId") Long id) {
        List<TbBusinessTrackRecord> list= tbBusinessTrackRecordService.selectTbBusinessTrackRecordList(id);
        for (TbBusinessTrackRecord businessTrackRecord : list) {
            String[] items= businessTrackRecord.getKeyItems().split(",");
            for (String item : items) {
                String dictLable= sysDictDataService.selectDictLabel("communication_point",item);
                businessTrackRecord.getKeys().add(dictLable);
            }
        }
        return AjaxResult.success(list);
    }


    @PreAuthorize("@ss.hasPermi('clues:clue:add')")
    @Log(title = "线索跟进记录对象", businessType = BusinessType.INSERT)
    @PostMapping("/clues/record")
    @Transactional
    public AjaxResult add(@RequestBody ClueTrackRecordVo clueTrackRecord) {
        TbClue tbClue = new TbClue();
        tbClue.setId(clueTrackRecord.getClueId());
        tbClue.setQq(clueTrackRecord.getQq());
        tbClue.setWeixin(clueTrackRecord.getWeixin());
        tbClue.setName(clueTrackRecord.getName());
        tbClue.setSex(clueTrackRecord.getSex());
        tbClue.setAge(clueTrackRecord.getAge());
        tbClue.setStatus("2");
        tbClue.setNextTime(clueTrackRecord.getNextTime());
        tbClueService.updateTbClue(tbClue);

        clueTrackRecordService.insertTbClueTrackRecord(clueTrackRecord);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('clues:clue:list')")
    @GetMapping("/clues/record/list")
    public RecordVo list(TbClueTrackRecord clueTrackRecord) {

        return clueTrackRecordService.selectClueTrackRecordById(clueTrackRecord);
    }

}
